package com.shiguiwu.springmybatis.designpattern.abstractfactory.scene.factory;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @description: jdk代理
 * @author: stone
 * @date: Created by 2021/8/12 23:22
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.designpattern.abstractfactory.scene.factory
 */
public class JDKProxy {

    public static <T> T getProxy(Class<T> interfaceClass, ICacheAdapter cacheAdapter) throws Exception {
        InvocationHandler handler = new JDKInvocationHandler(cacheAdapter);
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class<?>[] classes = interfaceClass.getInterfaces();
        return (T) Proxy.newProxyInstance(classLoader, new Class[]{classes[0]}, handler);
    }
}
